package com.thirteen.app.controller;

import com.thirteen.app.dto.CreateExamRequest.ExamDto;
import com.thirteen.app.dto.ExamConditionDto;
import com.thirteen.app.dto.PaperListDto;
import com.thirteen.app.entity.Exam;
import com.thirteen.app.service.CourseService;
import com.thirteen.app.service.ExamRecordService;
import com.thirteen.app.service.ExamService;
import com.thirteen.app.service.PaperService;
import com.thirteen.app.utils.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.oas.annotations.EnableOpenApi;

import javax.annotation.Resource;


@CrossOrigin
@Api(tags = "教师考试管理模块")
@EnableOpenApi
@RestController
@RequestMapping("/api")
public class ExamController {

    /**
     * 服务对象
     */
    @Resource
    private ExamService examService;

    @Resource
    private ExamRecordService examRecordService;

    @Resource
    private CourseService courseService;

    @Resource
    private PaperService paperService;

    @ApiOperation("根据考试查询试卷及考试学生")
    @GetMapping("getExamPaperList")
    public PaperListDto getExamPaperList(Integer examId) {
        return examService.getExamPaperList(examId);
    }

    @ApiOperation("学生查询考试")
    @GetMapping("/student/exam/selectAllExam")
    public CommonResult queryAllExamStudent(ExamConditionDto examConditionDto) {
        return CommonResult.success(examService.queryAllExamStudent(examConditionDto));
    }

    @ApiOperation("教师查询考试")
    @GetMapping("/teacher/exam/selectByTeacher")
    public CommonResult queryAllExamTeacher(ExamConditionDto examConditionDto) {
        return CommonResult.success(examService.queryAllExamTeacher(examConditionDto));
    }

    @ApiOperation("根据Id查询考试")
    @GetMapping("/student/exam/selectById")
    public CommonResult selectExamById(Integer Id) {
        Exam exam = examService.queryById(Id);
        exam.setCourseName(courseService.queryById(exam.getCourseId()).getCourseName());
        return CommonResult.success(exam);
    }

    @ApiOperation("列表查询获取课程和其相关试卷")
    @GetMapping("/teacher/exam/getCourseAndPaper")
    public CommonResult queryAllCourseAndPaper() {
        return CommonResult.success(courseService.queryAllCourseAndPaper());
    }

    @ApiOperation("获取试卷简略信息")
    @GetMapping("/teacher/exam/getPaperInfo")
    public CommonResult getPaper(Integer paperId) {
        System.out.println(paperService.queryPaperInfo(paperId).toString());
        return CommonResult.success(paperService.queryPaperInfo(paperId));
    }

    @ApiOperation("创建考试")
    @PostMapping("/teacher/exam/createExam")
    public CommonResult createExam(@RequestBody ExamDto examDto) {
        System.out.println(examDto.toString());
        examService.insertExam(examDto);
        return CommonResult.success(examDto);
    }

    /**
     * wc
     */

//    @ApiOperation("通过名称查询考试信息")
//    @GetMapping("/teacher/exam/selectByName")
//    public CommonResult queryExamByName(String examName, Integer userId) {
//        return CommonResult.success(examService.queryExamByName(examName, userId));
//    }

//    @ApiOperation("教师端全查考试信息")
//    @GetMapping("/list")
//    public ApiRest<List<Exam>> listExam() {
//        return super.success(this.examService.queryAll());
//    }

    @ApiOperation("学生端查询用户考试信息")
    @GetMapping("/student/exam/selectByUser")
    public CommonResult selectByUser(ExamConditionDto examConditionDto) {
        return CommonResult.success(examService.queryAllByUser(examConditionDto));
    }

    @ApiOperation("教师端时区全查")
    @GetMapping("/teacher/exam/queryAllByTeacherTime")
    public CommonResult queryAllByTeacherTime(String beginTime, String endTime, String examName, Integer userId){
        return CommonResult.success(examService.queryAllByTeacherTime(beginTime, endTime, examName, userId));
    }

    @ApiOperation("查询某次考试所有未批阅试卷")
    @GetMapping("/teacher/exam/queryAllUnJudgeByExamId")
    public CommonResult queryAllUnJudgeByExamId(Integer examId, Integer userId) {
        return CommonResult.success(examRecordService.queryAllUnJudgeByExamId(examId, userId));
    }

    @ApiOperation("查询某次考试所有试卷")
    @GetMapping("/teacher/exam/queryAllByExamId")
    public CommonResult queryAllByExamId(Integer examId,String realName) {
        return CommonResult.success(examRecordService.queryAllByExamId(examId,  realName));
    }

    @ApiOperation("管理员分页全查考试")
    @GetMapping("/admin/exam/exam/pageList")
    public CommonResult findExamPage(String examName,String courseName,Integer pageIndex, Integer pageSize) {
        return CommonResult.success(examService.queryAllExamByLimit(examName,courseName,pageIndex, pageSize));
    }

    @ApiOperation("删除考试")
    @PostMapping("/admin/exam/exam/delete")
    public CommonResult deleteExam(@RequestBody Exam exam) {
        return CommonResult.success(examService.deleteById(exam.getExamId()));
    }

    @ApiOperation("更新考试信息")
    @PostMapping("/edit")
    public CommonResult updateExam(Exam exam) {
        return CommonResult.success(examService.update(exam));
    }

    @ApiOperation("管理员审核考试")
    @PostMapping("/admin/exam/exam/changeAudit")
    public CommonResult judgeExamAuditAdmin(Integer examId,Integer audit){
        return CommonResult.success(examService.judgeExamAuditAdmin(examId,audit));
    }

    @ApiOperation("老师提交审核考试")
    @PostMapping("/teacher/exam/changeAudit")
    public CommonResult applyExamAuditTeacher(Integer paperId){
        return CommonResult.success(examService.applyExamAuditTeacher(paperId));
    }

}
